package com.gdth.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.List;

import com.gdth.model.TreeItem;
import com.gdth.widget.ExpandableListView.TreeViewAdapter.TreeNode;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;

public class TreeViewAdapterForSuper extends BaseExpandableListAdapter implements OnChildClickListener{  
	
	private Context parentContext;  
	
    /**
     * 外部回调函数
     */
    private OnChildClickListener stvClickEvent;
    
    /**
     * 数据源
     */
    private List<SuperTreeNode> superTreeNodes = new ArrayList<SuperTreeNode>();
    
	private AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TreeViewAdapter.ItemHeight);
    
    public TreeViewAdapterForSuper(Context view, OnChildClickListener stvClickEvent) {  
        parentContext = view;  
        this.stvClickEvent=stvClickEvent;  
    }  
  
    
    
    /****************Group******************/

    public int getGroupCount() {  
        return superTreeNodes.size();  
    }  
  
    public long getGroupId(int groupPosition) {  
        return groupPosition;  
    }  
    
    public Object getGroup(int groupPosition) {  
        return superTreeNodes.get(groupPosition).parent;  
    }
    
    /** 
     * 三级树结构中的首层是TextView,用于作为title 
     */  
    public View getGroupView(int groupPosition, boolean isExpanded,  
            View convertView, ViewGroup parent) {  
        TextView textView = TreeViewAdapter.getTextView(this.parentContext);  
        textView.setText(getGroup(groupPosition).toString());  
        textView.setPadding(TreeViewAdapter.PaddingLeft, 0, 0, 0);  
        return textView;  
    }
    
    
    
    /****************Child******************/
    public int getChildrenCount(int groupPosition) {  
        return superTreeNodes.get(groupPosition).childs.size();  
    } 
    
    public long getChildId(int groupPosition, int childPosition) {  
        return childPosition;  
    }  
    
    public Object getChild(int groupPosition, int childPosition) {  
        return superTreeNodes.get(groupPosition).childs.get(childPosition);  
    }
  
    /** 
     * 三层树结构中的第二层是一个ExpandableListView 
     */   
	public View getChildView(int groupPosition, int childPosition,  
            boolean isLastChild, View convertView, ViewGroup parent) {  
        // 是   
        
        String key = superTreeNodes.get(groupPosition).parent.toString();
        
        final TreeNode treeNode=(TreeNode) getChild(groupPosition, childPosition);
        
        if (treeNode.childs == null){
        	
            TextView textView = new TextView(this.parentContext);  
            textView.setLayoutParams(lp);  
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            
            TreeItem nodes = (TreeItem) treeNode.parent;
            textView.setText(nodes.val);  
            textView.setPadding(TreeViewAdapter.PaddingLeft, 0, 0, 0);  
            textView.setTag(nodes.key);
            return textView;  
        }
        else{
        	final ExpandableListView treeView = getExpandableListView();
	        final TreeViewAdapter treeViewAdapter = new TreeViewAdapter(this.parentContext,0, stvClickEvent, key);
	        //临时变量取得TreeViewAdapter的TreeNode集合，可为空  
	        List<TreeNode> tmp = treeViewAdapter.GetTreeNode();
	          
	
	        tmp.add(treeNode);  
	        treeViewAdapter.UpdateTreeNode(tmp);  
	        treeView.setAdapter(treeViewAdapter);  
	          
	        //关键点：取得选中的二级树形菜单的父子节点,结果返回给外部回调函数  
	        treeView.setOnChildClickListener(this.stvClickEvent);  
	          
	        /** 
	         * 关键点：第二级菜单展开时通过取得节点数来设置第三级菜单的大小 
	         */  
	        treeView.setOnGroupExpandListener(new OnGroupExpandListener() {  
	            @Override  
	            public void onGroupExpand(int groupPosition) { 
	                AbsListView.LayoutParams lps = new AbsListView.LayoutParams(  
	                        ViewGroup.LayoutParams.MATCH_PARENT,  
	                        (treeNode.childs.size()+1)*TreeViewAdapter.ItemHeight + 10);  
	                treeView.setLayoutParams(lps);
	            }  
	        });  
	          
	        /** 
	         * 第二级菜单回收时设置为标准Item大小 
	         */  
	        treeView.setOnGroupCollapseListener(new OnGroupCollapseListener() {  
	            
				@Override  
	            public void onGroupCollapse(int groupPosition) { 
	                treeView.setLayoutParams(lp);
	            }  
	        });  
	        treeView.setPadding(TreeViewAdapter.PaddingLeft, 0, 0, 0);  
	        return treeView;  
        }
    }  
	
	public ExpandableListView getExpandableListView() {   
        ExpandableListView superTreeView = new ExpandableListView(parentContext);  
        superTreeView.setLayoutParams(lp);  
        return superTreeView;  
    } 
	
	
  
    public boolean isChildSelectable(int groupPosition, int childPosition) {  
        return true;  
    }  
  
    public boolean hasStableIds() {  
        return true;  
    }

	@Override
	public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
			int arg3, long arg4) {
		// TODO Auto-generated method stub
		
		return false;
	}  
	
	
	
	/****************SuperTreeNode******************/
	static public class SuperTreeNode {  
        public Object parent;  
        //二级树形菜单的结构体  
        public List<TreeViewAdapter.TreeNode> childs = new ArrayList<TreeViewAdapter.TreeNode>();  
    }
	
	public List<SuperTreeNode> GetTreeNode() {  
        return superTreeNodes;  
    }  
  
    public void UpdateTreeNode(List<SuperTreeNode> node) {  
        superTreeNodes = node;  
    }  
      
    public void RemoveAll()  
    {  
        superTreeNodes.clear();  
    }  
}
